<?php

namespace addon\ly_shareholder\app\listener\member;

use addon\ly_shareholder\app\model\Shareholder;
use addon\ly_shareholder\app\service\core\CoreShareholderLevelService;
use addon\ly_shareholder\app\service\core\CoreShareholderService;
use app\model\member\Member;
use think\facade\Log;

/**
 * 会员注册成功事件
 * Class MemberRegister
 * @package addon\ly_lian\app\listener\member
 */
class MemberRegisterListener
{
    /**
     * 接收添加会员的数组参数
     * @param $member  ['member_id', 'pid']
     */
    public function handle($member)
    {
        Log::write('===============会员注册事件===============' . json_encode($member));
        // 检查会员是否存在
        if (empty($member) || empty($member['member_id']) || empty($member['site_id'])) {
            return;
        }
        
        // 直接调用统一的股东升级处理方法
        CoreShareholderLevelService::shareholderUpgrade($member['site_id'], $member['member_id']);
        
        // 如果会员有上级，也为上级检查升级
        if (!empty($member['pid'])) {
            CoreShareholderLevelService::shareholderUpgrade($member['site_id'], $member['pid']);

            // 更新会员的团队人数及分红权益
            (new CoreShareholderService())->updateTeamCount($member['site_id'], $member);
        }
    }
}